package com.bkgy.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bkgy.modular.datacollect_bx.bean.*;
import com.bkgy.modular.datacollect_bx.dao.*;
import com.bkgy.controller.tbbxrecord.dao.TbBxFileSaveDao;
import com.bkgy.controller.tbbxrecord.entity.TbBxFileSave;
import com.bkgy.modular.tbdsjdatacollect.dao.TbDsjDataCollectDao;
import com.bkgy.modular.tbdsjdatacollect.entity.TbDsjDataCollect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 保存宝信sft文件数据时，同时存储PDO数据；从宝信DB2中获取
 */
@Service
@Slf4j
public class PdoUtils {

    @Resource
    private JColdPdo621Dao jColdPdo621Dao;
    @Resource
    private JColdPdo622Dao jColdPdo622Dao;
    @Resource
    private JColdPdo642Dao jColdPdo642Dao;
    @Resource
    private JColdPdo621MongoDao jColdPdo621MongoDao;
    @Resource
    private JColdPdo622MongoDao jColdPdo622MongoDao;
    @Resource
    private JColdPdo642MongoDao jColdPdo642MongoDao;
    @Resource
    private TbDsjDataCollectDao tbDsjDataCollectDao;
    @Resource
    private PojoUtils pojoUtils;
    @Resource
    private MongoTemplate mongoTemplate;
    @Resource
    private TbBxFileSaveDao tbBxFileSaveDao;

    public void calPdo621(String volume,TbBxFileSave tbBxFileSave){
        try {
            Map<String, Object> mapResult = new HashMap<>();
            QueryWrapper<JColdPdo621> wrapper = new QueryWrapper<>();
            wrapper.eq("extid",volume);
            JColdPdo621 jColdPdo621 = jColdPdo621Dao.selectOne(wrapper);
            QueryWrapper<TbDsjDataCollect> wrapperMongo = new QueryWrapper<>();
            wrapperMongo.eq("PRODUCTION_LINE","C621_PDO");
            wrapperMongo.eq("out_data_source",5);
            List<TbDsjDataCollect> tbDsjDataCollects = tbDsjDataCollectDao.selectList(wrapperMongo);
            Map<String, Object> map = pojoUtils.beanToMap(jColdPdo621);
            tbDsjDataCollects.forEach(t->{
                if(!StringUtils.isEmpty(map.get(t.getL1_variable_name().toLowerCase()))){
                    mapResult.put(t.getL1_variable_alias(),map.get(t.getL1_variable_name().toLowerCase()));
                }
            });
            JColdPdoMongo621 pdoMongo621 = new JColdPdoMongo621();
            pdoMongo621.setVolumeNo(volume);
            pdoMongo621.setRow(mapResult);

            // 创建条件查询器
            Query query = new Query(Criteria.where("volumeNo").is(volume));
            List<JColdPdoMongo621> jColdPdoMongo621List = mongoTemplate.find(query, JColdPdoMongo621.class);
            // 判断根据卷号查询出的数据是否为null,为null保存
            if (jColdPdoMongo621List.size() > 0) {
                return;
            } else {
                jColdPdo621MongoDao.save(pdoMongo621);
            }
        }catch (Exception e){
            tbBxFileSave.setStatuspdo(1.0);
            UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(TbBxFileSave::getCoil_id,tbBxFileSave.getCoil_id());
            tbBxFileSaveDao.update(tbBxFileSave,updateWrapper);
            log.error("c621冷轧PDO处理失败");
            e.printStackTrace();
        }
    }
    public void calPdo622(String volume, TbBxFileSave tbBxFileSave){
       try {
           Map<String, Object> mapResult = new HashMap<>();
           QueryWrapper<JColdPdo622> wrapper = new QueryWrapper<>();
           wrapper.eq("extid",volume);
           JColdPdo622 jColdPdo622 = jColdPdo622Dao.selectOne(wrapper);
           QueryWrapper<TbDsjDataCollect> wrapperMongo = new QueryWrapper<>();
           wrapperMongo.eq("PRODUCTION_LINE","C622_PDO");
           wrapperMongo.eq("out_data_source",5);
           List<TbDsjDataCollect> tbDsjDataCollects = tbDsjDataCollectDao.selectList(wrapperMongo);
           Map<String, Object> map = pojoUtils.beanToMap(jColdPdo622);
           tbDsjDataCollects.forEach(t->{
               if(!StringUtils.isEmpty(map.get(t.getL1_variable_name().toLowerCase()))){
                   mapResult.put(t.getL1_variable_alias(),map.get(t.getL1_variable_name().toLowerCase()));
               }
           });
           JColdPdoMongo622 pdoMongo622 = new JColdPdoMongo622();
           pdoMongo622.setVolumeNo(volume);
           pdoMongo622.setRow(mapResult);


           // 创建条件查询器
           Query query = new Query(Criteria.where("volumeNo").is(volume));
           List<JColdPdoMongo622> jColdPdoMongo622List = mongoTemplate.find(query, JColdPdoMongo622.class);
           // 判断根据卷号查询出的数据是否为null,为null保存
           if (jColdPdoMongo622List.size() > 0) {
               return;
           } else {
               jColdPdo622MongoDao.save(pdoMongo622);
           }
       }catch (Exception e){
           tbBxFileSave.setStatuspdo(1.0);
           UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
           updateWrapper.lambda().eq(TbBxFileSave::getCoil_id,tbBxFileSave.getCoil_id());
           tbBxFileSaveDao.update(tbBxFileSave,updateWrapper);
           log.error("c622冷轧PDO处理失败");
           e.printStackTrace();
       }
    }
    public void calPdo642(String volume, TbBxFileSave tbBxFileSave){
       try {
           Map<String, Object> mapResult = new HashMap<>();
           QueryWrapper<JColdPdo642> wrapper = new QueryWrapper<>();
           wrapper.eq("extid",volume);
           JColdPdo642 jColdPdo642 = jColdPdo642Dao.selectOne(wrapper);
           QueryWrapper<TbDsjDataCollect> wrapperMongo = new QueryWrapper<>();
           wrapperMongo.eq("PRODUCTION_LINE","C642_PDO");
           wrapperMongo.eq("out_data_source",5);
           List<TbDsjDataCollect> tbDsjDataCollects = tbDsjDataCollectDao.selectList(wrapperMongo);
           Map<String, Object> map = pojoUtils.beanToMap(jColdPdo642);
           tbDsjDataCollects.forEach(t->{
               if(!StringUtils.isEmpty(map.get(t.getL1_variable_name().toLowerCase()))){
                   mapResult.put(t.getL1_variable_alias(),map.get(t.getL1_variable_name().toLowerCase()));
               }
           });
           JColdPdoMongo642 pdoMongo642 = new JColdPdoMongo642();
           pdoMongo642.setVolumeNo(volume);
           pdoMongo642.setRow(mapResult);
           jColdPdo642MongoDao.save(pdoMongo642);
       }catch (Exception e){
           tbBxFileSave.setStatuspdo(1.0);
           UpdateWrapper<TbBxFileSave> updateWrapper = new UpdateWrapper<>();
           updateWrapper.lambda().eq(TbBxFileSave::getCoil_id,tbBxFileSave.getCoil_id());
           tbBxFileSaveDao.update(tbBxFileSave,updateWrapper);
           log.error("c642冷轧PDO处理失败");
           e.printStackTrace();
       }
    }

}
